#include"../include/lib_headers.h"
label* recreate_dir(fd *dir) {	
	char parent[SIZE] = "",*dt, *s, input[SIZE] = "", *split_char, *p[SIZE], copy[SIZE] = "";
	int i = 0, j = 0, k = 0, match = 0, t = 0, y = 1;
	node *temp, *prev;

	dt = add_slash(dir->fullPath);
	strcpy(parent, dt);

	split_char = "/";
	i = splits_directory(parent, p, split_char);

	if (l1->next == NULL) {
		j = 0;
		node *n = (node*) malloc(sizeof(node));
		fd *f= (fd*) malloc(sizeof(fd));
		n->fd_pointer=f;
		strcpy(f->fileName, p[j]);
		strcpy(f->fullPath, "/");
		strcat(f->fullPath, p[j]);
		/* strcat(f->fullPath, "/"); */
		strcpy(copy, p[j]);
		strcat(copy, "/");
		l1->next = n;
		n->child = NULL;
		f->blockno= dir->blockno;
		n->sibling = NULL;
		strcpy(f->fileType,dir->fileType);
		f->fileSize = dir->fileSize;
		j++;
		temp = n;
		while (j < i) {
			node *n = (node*) malloc(sizeof(node));
			fd *f= (fd*) malloc(sizeof(fd));
			n->fd_pointer=f;
			strcat(copy, "/");
			strcat(copy, p[j]);
			/* strcat(copy, "/"); */
			strcpy(f->fileName, p[j]);
			strcpy(f->fullPath, copy);
			temp->child = n;
			n->child = NULL;
			f->blockno= dir->blockno;
			n->sibling = NULL;
			strcpy(f->fileType,dir->fileType);
			f->fileSize = dir->fileSize;
			temp = n;
			j++;
		}
		return l1;
	}

	temp = l1->next;
	strcpy(copy, "");
	split_char = "/";
	i = splits_directory(parent, p, split_char);
	match = 0;

	if (l1->next != NULL) {
		j = 0;
		t = 0;
		temp = l1->next;
		while (j < i) {
			while (temp != NULL) {
				if (strcmp(temp->fd_pointer->fileName, p[j]) == 0) {
					match = 1;
					strcat(copy, "/");
					strcat(copy, p[j]);
					/* strcat(copy, "/"); */
					j++;
					prev = temp;
					temp = temp->child;
					if (temp == NULL) {
						t = 1;
					}
					if(j==i)
						break;
				}
				if (match == 0) {
					prev = temp;
					temp = temp->sibling;
				} else if (match == 1) {
					match = 0;
					continue;
				}
			}
			if (temp == NULL) {
				strcat(copy, "/");
				strcat(copy, p[j]);
				/* strcat(copy, "/"); */
				node *n = (node*) malloc(sizeof(node));
				fd *f= (fd*) malloc(sizeof(fd));
				n->fd_pointer=f;
				n->child = NULL;
				f->blockno= dir->blockno;
				n->sibling = NULL;
				strcpy(f->fileType,dir->fileType);
				strcpy(f->fileName, p[j]);
				strcpy(f->fullPath, copy);
				f->fileSize = dir->fileSize;
				/* terminated because of no child */
				if (t == 1) { 
					prev->child = n;
				}
				/* terminated because end of linked list */ 
				else if (t == 0) { 
					prev->sibling = n;
					t = 1;
				}
				j++;
				temp = n;
			}
		}
		return l1;
	}
}
